package com.zygh.lz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.dao.*;
import com.zygh.lz.entity.*;
import com.zygh.lz.service.LocationService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LocationServiceImpl implements LocationService {
    @Resource
    private LocationMapper locationMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private EquipageMapper equipageMapper;
    @Resource
    private PatrolrecordMapper patrolrecordMapper;
    @Resource
    private RegisterMapper registerMapper;


    @Override
    public ResultBean addLocation(Location location) {

        //如果定位异常存入
        if ("GPS异常".equals(location.getLocationDescribe())) {
            Register register = new Register();
            register.setUsername(location.getWarningSignal());
            Staff staff = staffMapper.selectStaffByNum(location.getWarningSignal());
            register.setUserpass(staff.getStaffName());
            register.setImei(location.getLocationDescribe());
            register.setFlag(DateUtil.now());
            registerMapper.insert(register);
            return null;

        }


        Equipage equipage = new Equipage();
        equipage.setUploadTime(DateUtil.now());
        equipage.setLongitude(location.getLongitude());
        equipage.setLatitude(location.getLatitude());
        equipage.setWarningSignal(location.getWarningSignal());
        equipage.setDirection(location.getDirection());
        equipage.setDistrict(location.getDistrict());
        equipage.setTown(location.getTown());
        equipage.setSpeed(location.getSpeed());
        equipage.setLocationDescribe(location.getLocationDescribe());
        equipageMapper.update(equipage, Wrappers.<Equipage>lambdaQuery()
                .eq(Equipage::getWarningSignal, location.getWarningSignal())
                .and(wrapper->wrapper.like(Equipage::getType, "通")
                        .or().like(Equipage::getType, "辅警")));


        //更新巡查时间
        Patrolrecord patrolrecord = patrolrecordMapper.selectrecordByid(location.getStaffid());
        if (patrolrecord != null) {
            patrolrecord.setPatrolRecordEndtime(new Date());
            patrolrecordMapper.updateByPrimaryKeySelective(patrolrecord);
        }
        return ResultUtil.execOp(locationMapper.addLocation(location), "新增");
    }

    @Override
    public ResultBean delLocation(Integer id) {
        return ResultUtil.execOp(locationMapper.delete(id), "删除");
    }

    @Override
    public ResultBean subordinatePosition(Integer id) {
        Staff staff = staffMapper.selectByPrimaryKey(id);
        if (staff.getStaffPost().contains("中队长")) {
            //查询本中队下人员的实时坐标（时间在30分钟内的位置）
            List<Location> locations = locationMapper.subordinatePosition(staff.getSysSectionId());
            return ResultUtil.setOK("success", locations);
        }
        return ResultUtil.setOK("success", null);
    }


}
